package test;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;

public class HFrame extends JFrame {

	/**
	 * HFrame
	 */
	private static final long serialVersionUID = 6195747504317051296L;

	public HFrame() {

	}

	public static void main(String args[]) {
		int[] rgb = new int[3];

		File file = new File("D:/pic/a.jpg");
		BufferedImage bi = null;
		try {
			bi = ImageIO.read(file);
		} catch (Exception e) {
			e.printStackTrace();
		}

		int width = bi.getWidth();
		int height = bi.getHeight();
		int minx = bi.getMinX();
		int miny = bi.getMinY();
		System.out.println("width=" + width + ",height=" + height + ".");
		System.out.println("minx=" + minx + ",miniy=" + miny + ".");
		int hst[] = new int[256];
		int max = 0;
		for (int i = minx; i < width; i++) {
			for (int j = miny; j < height; j++) {
				// System.out.print(bi.getRGB(jw, ih));
				int pixel = bi.getRGB(i, j);
				int r = rgb[0] = (pixel & 0xff0000) >> 16;
				int g = rgb[1] = (pixel & 0xff00) >> 8;
				int b = rgb[2] = (pixel & 0xff);
				// System.out.println("i=" + i + ",j=" + j + ":(" + rgb[0] +
				// ","+ rgb[1] + "," + rgb[2] + ")");
				 int q=(r+g+b)/3;
				//int q = (int) (r * 0.3 + g * 0.59 + b * 0.11);
				//  q=0;
				// q=-1*q+255;
				
				// q = Math.max(r, Math.max(g, b));
				bi.setRGB(i, j,  (q<<16)+(q <<8)+q);
				hst[q]++;

			}
		}
		for (int i = 0; i < 256; i++) {
			if (max <=hst[i]) {
				max = hst[i];
			}
		}
		try {
			ImageIO.write(bi, "jpg", new File("d:/pic/c.jpg"));
			System.out.println("ok");
			BufferedImage image = new BufferedImage(257, 257,
					BufferedImage.TYPE_INT_RGB);
			Graphics2D g2 = image.createGraphics();
			for (int i = 0; i < hst.length; i++) {
				int size = (int)(256* hst[i] /max +0.001);
				int x1 = i;
				int y1 = 256;
				int x2 = i;
				int y2 = 256 - size;
				g2.drawLine(x1, y1, x2, y2);
			}
			ImageIO.write(image, "JPG", new File("d:/pic/129.jpg"));
			System.out.println("ok");
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}
